package com.wang.lottery.infrastucture.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.lottery.domain.activity.model.po.Award;
import com.wang.lottery.domain.strategy.model.aggregates.StrategyRich;
import com.wang.lottery.domain.strategy.model.po.Strategy;
import com.wang.lottery.domain.strategy.model.po.StrategyDetail;
import com.wang.lottery.domain.strategy.model.vo.StrategyDetailVO;
import com.wang.lottery.domain.strategy.repository.IStrategyRepository;
import com.wang.lottery.infrastucture.mapper.AwardMapper;
import com.wang.lottery.infrastucture.mapper.StrategyDetailMapper;
import com.wang.lottery.infrastucture.mapper.StrategyMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjh
 * @create 2023-11-01 14:40
 * 数据仓储实现类
 */
@Component
public class StrategyRepository extends ServiceImpl<StrategyMapper, Strategy> implements IStrategyRepository {

    @Resource
    private StrategyMapper strategyMapper;
    @Resource
    private StrategyDetailMapper strategyDetailMapper;
    @Resource
    private AwardMapper awardMapper;
    
    /**
     * 查询策略配置
     *
     * @param strategyId 策略id
     * @return 策略和策略详细一起返回
     */
    @Override
    public StrategyRich queryStrategyRich(Long strategyId) {
        LambdaQueryWrapper<Strategy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(strategyId != null,Strategy::getStrategyId,strategyId);
        Strategy strategy = strategyMapper.selectOne(queryWrapper);
        LambdaQueryWrapper<StrategyDetail> strategyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        strategyLambdaQueryWrapper.eq(strategyId != null,StrategyDetail::getStrategyId,strategyId);
        List<StrategyDetail> strategyDetails = strategyDetailMapper.selectList(strategyLambdaQueryWrapper);
        return new StrategyRich(strategyId,strategy,strategyDetails);
    }

    /**
     * 添加策略明细配置
     *
     * @param strategyDetailVOList 策略明细集合
     */
    @Override
    public void addStrategyDetailList(List<StrategyDetailVO> strategyDetailVOList) {
        List<StrategyDetail> strategyDetailList = new ArrayList<>();
        for (StrategyDetailVO strategyDetailVO : strategyDetailVOList) {
            StrategyDetail strategyDetail = new StrategyDetail();
            BeanUtils.copyProperties(strategyDetailVO, strategyDetail);
            strategyDetailList.add(strategyDetail);
        }
        strategyDetailMapper.insertBatch(strategyDetailList);
    }

    /**
     * 查询奖品信息
     *
     * @param awardId 奖品id
     * @return 奖品信息
     */
    @Override
    public Award queryAwardInfo(String awardId) {
        LambdaQueryWrapper<Award> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Award::getAwardId,awardId);
        return awardMapper.selectOne(queryWrapper);
    }

    /**
     * 查询没有库存的奖品id列表
     *
     * @param strategyId 策略id
     * @return 无库存的奖品id列表
     */
    @Override
    public List<String> queryNoStockStrategyAwardList(Long strategyId) {
        LambdaQueryWrapper<StrategyDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrategyDetail::getStrategyId,strategyId).eq(StrategyDetail::getAwardSurplusCount,0);
        return strategyDetailMapper.selectList(queryWrapper).stream().map(StrategyDetail::getAwardId).collect(Collectors.toList());
    }

    /**
     * 扣减库存
     *
     * @param strategyDetailReq 策略ID、奖品ID
     * @return 返回结果
     */
    @Override
    public boolean deductStock(StrategyDetail strategyDetailReq) {
        LambdaUpdateWrapper<StrategyDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setSql("award_surplus_count = award_surplus_count - 1")
                .eq(StrategyDetail::getStrategyId,strategyDetailReq.getStrategyId())
                .eq(StrategyDetail::getAwardId,strategyDetailReq.getAwardId());
        int update = strategyDetailMapper.update(strategyDetailReq, updateWrapper);
        return update == 1;
    }
}
